<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * user edit
 *
 * @package GeoContexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 828 $ / $LastChangedDate: 2011-02-27 10:30:28 +0100 (So, 27 Feb 2011) $ / $Author: armand.turpel $
 */

namespace Geocontexter\Controller;

use Zend\View\Model\ViewModel;
use Core\Controller\AbstractController;

class UserEditController extends AbstractController
{
   public function init()
    {
        $this->initView( 'geocontexter/user-edit/index.phtml' );

        $this->view->partialData                = array('active_page' => 'edit');
        $this->view->timezone_identifiers       = \DateTimeZone::listIdentifiers();
        $this->view->error                      = array();
        $this->view->user_result                = array();
    }

    private function init_data()
    {
        $LanguagesGet = $this->CoreModel('LanguagesGet');
        $params = array('enable' => 'true');

        $this->view->languages = $LanguagesGet->run( $params );
    }

    public function indexAction()
    {
        $this->view->id_user = $this->id_user = $this->params()->fromRoute('id_user',false);

        if ((null === $this->id_user) || (0 == $this->id_user)) {

            $this->id_user = 0;
            $this->renderer->headTitle('Edit user', 'PREPEND');

        } else {

            $UserGet = $this->CoreModel('UserGet');
            $params  = array('id_user' => $this->id_user);
            $result  = $UserGet->run( $params );

            $this->view->user_result = $result;

            $this->renderer->headTitle('Edit user ' . $result['user_forename'] . ' ' . $result['user_lastname'], 'PREPEND');
        }

        $this->init_data();

        return $this->view;
    }

    /**
     * add new group action
     */
    public function updateAction()
    {
        // check on cancel action
        //
        $cancel = $this->request->getPost()->cancel;

        if ($cancel !== null) {
            return $this->redirect()->toUrl($this->getAdminBaseUrl() . '/user/index');
        }

        $params      = array();
        $error       = array();
        $user_result = array();

        $this->view->id_user = $this->id_user = $this->request->getPost()->id_user;

        $user_result['user_login'] = $this->request->getPost()->user_login;

        $user_result['id_status']        =
            $params['id_status']      = $this->request->getPost()->id_status;

        $user_result['id_group']         =
            $params['id_group']       = $this->request->getPost()->id_group;

        $user_result['user_lastname']    =
            $params['user_lastname']  = $this->request->getPost()->user_lastname;

        $user_result['user_forename']    =
            $params['user_forename']  = $this->request->getPost()->user_forename;

        $user_result['user_lang']        =
            $params['lang']           = $this->request->getPost()->user_lang;

        $user_result['user_email']       =
            $params['user_email']     = $this->request->getPost()->user_email;

        $user_result['user_url']         =
            $params['user_url']       = $this->request->getPost()->user_url;

        $user_result['user_timezone']    =
            $params['user_timezone']  = $this->request->getPost()->user_timezone;

        $user_result['user_description'] =
            $params['description']    = $this->request->getPost()->user_description;

        $user_password                = $this->request->getPost()->user_password;
        $user_password2               = $this->request->getPost()->user_password2;

        if (!empty($user_password)) {
            if ($user_password != $user_password2) {
                $error[] = 'Both user password must be equal';
            } else {
                $params['user_password'] = md5($user_password);
            }
        }

        // if status is trash then check if there is no related project
        //
        if ($params['id_status'] == 0) {

            $UserRelatedContentCheck = $this->CoreModel('UserRelatedContentCheck');
            $result = $UserRelatedContentCheck->run( $this->id_user );

            if (count($result) > 0) {
                $message = 'Couldnt move this user to trash. There are some records related to this user.';
                $this->view->error = array($message);
                $this->renderer->headTitle($message, 'PREPEND');
                $this->view->user_result = $user_result;
                return $this->view;
            }
        }

        // basic check if user dosent modify its own status
        // or modify other administrators account
        //
        if (false === $this->_check( $params )) {
            $error[] = 'Unexpected error';
        }

        if (empty($params['user_lastname'])) {
            $error[] = 'User lastname is empty';
        }
        if (empty($params['user_forename'])) {
            $error[] = 'User forename is empty';
        }
        if (empty($params['user_email'])) {
            $error[] = 'User email is empty';
        }

        if (count($error) > 0) {
            $this->renderer->headTitle('Error: User data failure', 'PREPEND');
            $this->view->error = $error;
            $this->view->user_result = $user_result;
            return $this->view;
        }

        $UserUpdate = $this->CoreModel('UserUpdate');

        // add item attribute
        //
        $result = $UserUpdate->run( array('id_user' => $this->id_user,
                                          'data'    => $params ));

        if ($result instanceof \Exception) {
           return $this->error( $result->getErrorString(), __file__, __line__ );
        } else {
            $this->redirect()->toUrl($this->getAdminBaseUrl() . '/user/index');
        }
    }

    private function _check( $params )
    {
        $identity = $this->get_identity();

        // cannot modify ouwn account status
        //
        if (($params['id_status']     < 200) &&
           ($identity->id_user == $params['id_user'])) {
            return false;
        }

        // cannot modify account of user of the same or higher rights group
        //
        if (($identity->id_group > 0) && ($identity->id_group >= $params['id_group'])) {
            return false;
        }

        return true;
    }
}

